package com.sangu.dianxiu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.bean.user.AddressEntity;
import com.sangu.dianxiu.utils.UserManager;

import org.simple.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;

/**
 * 常用联系人adapter，继承自AppBaseAdapter
 * Created by sg2 on 2016/1/8.
 */
public class FrequentAddressAdapter extends AppBaseAdapter<AddressEntity.AddrsEntity>{
    private UserManager userManager=new UserManager();
    public FrequentAddressAdapter(List<AddressEntity.AddrsEntity> list, Context context) {
        super(list, context);
    }

    @Override
    public ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        final AddressEntity.AddrsEntity entity=list.get(position);
        ViewHolder vh=ViewHolder.get(context,convertView,parent, R.layout.item_frequent_address_listview);
        TextView tvAddr= (TextView) vh.findViewById(R.id.tv_item_frequent_address_addr);
        TextView tvDefault= (TextView) vh.findViewById(R.id.tv_item_frequent_address_default);
        ImageView ivDelete= (ImageView) vh.findViewById(R.id.iv_item_frequent_address_delete);
        tvAddr.setText(entity.getAddr());
        if(!entity.isIsDefault())
        {
            tvDefault.setVisibility(View.GONE);             //如果不是默认联系人就让默认联系人标志隐藏
        }
        ivDelete.setOnClickListener(new View.OnClickListener() {        //点击删除按钮之后要更新地址列表，使用notifyDataSetChanged()进行通知;
            @Override
            public void onClick(View v) {
                HashMap<String,String> map=new HashMap<String, String>();
                map.put("userId", BaseApp.user.getUserId()+"");
                map.put("addrId",entity.getAddrId()+"");
                userManager.deleteAddress(map, new ICallBack() {        //删除地址要同时更新地址列表，使用notifyDataSetChanged();但是FrequentAddressActivity中的相应adapter应该注册观察者
                    @Override
                    public void onSuccess(String string) {
                    notifyDataSetChanged();
                    }

                    @Override
                    public void onError(VolleyError error) {

                    }
                });
            }
        });
        return vh;
    }
}
